package com.java110.mall.cart.bmo.inoutOrder.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.mall.cart.bmo.inoutOrder.IUpdateInoutOrderBMO;
import com.java110.core.annotation.Java110Transactional;
import com.java110.dto.inoutOrder.InoutOrderDto;
import com.java110.dto.product.ProductSpecValueDto;
import com.java110.intf.cart.IInoutOrderInnerServiceSMO;
import com.java110.intf.cart.IInoutOrderProductInnerServiceSMO;
import com.java110.intf.goods.IProductSpecValueInnerServiceSMO;
import com.java110.po.inoutOrder.InoutOrderPo;
import com.java110.po.inoutOrder.InoutOrderProductPo;
import com.java110.po.product.ProductSpecValuePo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("updateInoutOrderBMOImpl")
public class UpdateInoutOrderBMOImpl implements IUpdateInoutOrderBMO {

    @Autowired
    private IInoutOrderInnerServiceSMO inoutOrderInnerServiceSMOImpl;

    @Autowired
    private IInoutOrderProductInnerServiceSMO inoutOrderProductInnerServiceSMOImpl;

    @Autowired
    private IProductSpecValueInnerServiceSMO productSpecValueInnerServiceSMOImpl;
    /**
     * @param inoutOrderPo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> update(InoutOrderPo inoutOrderPo) {

        int flag = inoutOrderInnerServiceSMOImpl.updateInoutOrder(inoutOrderPo);

        if (flag > 0) {
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
    }

    /**
     * @param reqJson
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> saveOrderIn(JSONObject reqJson, String storeId) {
        String orderId = reqJson.getString("orderId");
        if (!reqJson.containsKey("inoutOrderProducts")) {
            throw new IllegalArgumentException("请求报文格式错误");
        }
        JSONArray inoutOrderProducts = reqJson.getJSONArray("inoutOrderProducts");
        if (ListUtil.isNull(inoutOrderProducts)) {
            throw new IllegalArgumentException("未包含商品");
        }
        InoutOrderProductPo inoutOrderProductPo = null;
        for (int inoutOrderProductIndex = 0; inoutOrderProductIndex < inoutOrderProducts.size(); inoutOrderProductIndex++) {
            if (inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("state").equals(InoutOrderDto.STATE_WI)){
                inoutOrderProductPo = new InoutOrderProductPo();
                //更新入库单中 实际入库数量
                inoutOrderProductPo.setActCount(inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("actCount"));
                inoutOrderProductPo.setItemId(inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("itemId"));
                inoutOrderProductPo.setState(InoutOrderDto.STATE_D);
                inoutOrderProductInnerServiceSMOImpl.updateInoutOrderProduct(inoutOrderProductPo);
                //更新库存中数量
                ProductSpecValuePo productSpecValuePo = new ProductSpecValuePo();
                productSpecValuePo.setStoreId(storeId);
                productSpecValuePo.setProductId(inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("productId"));
                productSpecValuePo.setValueId(inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("valueId"));
                productSpecValuePo.setAddStock(inoutOrderProductPo.getActCount());
                productSpecValueInnerServiceSMOImpl.updateProductSpecValue(productSpecValuePo);
            }
        }
        InoutOrderPo inoutOrderPo = new InoutOrderPo();
        inoutOrderPo.setOrderId(orderId);
        inoutOrderPo.setState(InoutOrderDto.STATE_D);
        int flag = inoutOrderInnerServiceSMOImpl.updateInoutOrder(inoutOrderPo);
        if (flag > 0) {
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }
        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
    }

    /**
     * @param reqJson
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> saveOrderOut(JSONObject reqJson, String storeId) {
        String orderId = reqJson.getString("orderId");
        if (!reqJson.containsKey("inoutOrderProducts")) {
            throw new IllegalArgumentException("请求报文格式错误");
        }
        JSONArray inoutOrderProducts = reqJson.getJSONArray("inoutOrderProducts");
        if (inoutOrderProducts == null || inoutOrderProducts.size() < 1) {
            throw new IllegalArgumentException("未包含商用");
        }
        InoutOrderProductPo inoutOrderProductPo = null;
        for (int inoutOrderProductIndex = 0; inoutOrderProductIndex < inoutOrderProducts.size(); inoutOrderProductIndex++) {
            if (inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("state").equals(InoutOrderDto.STATE_WO)){
                inoutOrderProductPo = new InoutOrderProductPo();
                //更新出库单中 实际出库数量
                inoutOrderProductPo.setActCount(inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("actCount"));
                inoutOrderProductPo.setShopId(inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("shopId"));
                inoutOrderProductPo.setValueId(inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("valueId"));
                inoutOrderProductPo.setProductId(inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("productId"));
                inoutOrderProductPo.setProdName(inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("prodName"));

                ProductSpecValueDto productSpecValueDto = new ProductSpecValueDto();
                productSpecValueDto.setShopId(inoutOrderProductPo.getShopId());
                productSpecValueDto.setValueId(inoutOrderProductPo.getValueId());
                productSpecValueDto.setProductId(inoutOrderProductPo.getProductId());
                List<ProductSpecValueDto> productSpecValueDtos = productSpecValueInnerServiceSMOImpl.queryProductSpecValues(productSpecValueDto);

                Assert.listOnlyOne(productSpecValueDtos, inoutOrderProductPo.getProdName()+"商品不存在");

                long stock = Long.parseLong(productSpecValueDtos.get(0).getStock());

                if (stock < Long.parseLong(inoutOrderProductPo.getActCount())) {
                    throw new IllegalArgumentException(inoutOrderProductPo.getProdName() + "库存不足");
                }

                inoutOrderProductPo.setItemId(inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("itemId"));
                inoutOrderProductPo.setState(InoutOrderDto.STATE_C);
                inoutOrderProductInnerServiceSMOImpl.updateInoutOrderProduct(inoutOrderProductPo);
                //减少库存中数量
                ProductSpecValuePo productSpecValuePo = new ProductSpecValuePo();
                productSpecValuePo.setStoreId(storeId);
                productSpecValuePo.setProductId(inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("productId"));
                productSpecValuePo.setValueId(inoutOrderProducts.getJSONObject(inoutOrderProductIndex).getString("valueId"));
                productSpecValuePo.setMinusStock(inoutOrderProductPo.getActCount());
                productSpecValueInnerServiceSMOImpl.updateProductSpecValue(productSpecValuePo);
            }
        }
        InoutOrderPo inoutOrderPo = new InoutOrderPo();
        inoutOrderPo.setOrderId(orderId);
        inoutOrderPo.setState(InoutOrderDto.STATE_C);
        int flag = inoutOrderInnerServiceSMOImpl.updateInoutOrder(inoutOrderPo);
        if (flag > 0) {
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }
        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
    }


}
